var Session = require('../../vendor/qcloud-weapp-client-sdk/lib/session.js');
var config = require('../../config.js');
var commons = require('../../commons.js');
var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
var register = require('../../template/register/register.js');
var utils = require('../../vendor/qcloud-weapp-client-sdk/lib/utils.js');
var index = {

  /**
   * 页面的初始数据
   */
  data: {
    showTanBox: false,
    dataOver: false, //下划线
    page: 1, //分页
    clickHideTanBox: false, //弹框
    shiPid: "",
    stautsPanDuan:'',
    listData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.indexListData();
  },
  // 开始直播
  beginZhiBo: function() {
    var id = this.data.shiPid;
    var detailID = this.data.listData[id].id;
    wx.navigateTo({
      url: '../details/details?detailID=' + detailID,
    })
  },
  // 修改直播
  clickAmend: function() {
    var id = this.data.shiPid;
    var detailID = this.data.listData[id].id;
    wx.navigateTo({
      url: 'NewZhiBo?detailID=' + detailID,
    })
  },
  // 复制推流地址
  copeUrl: function() {
    var self = this;
    qcloud.request({
      login: false,
      url: "https://live.xiaochengshi.net/Api/Article/beginLive",
      data: {
        id: self.data.listData[self.data.shiPid].id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (self.data.listData[self.data.shiPid].stauts == 1) {
          wx.setClipboardData({
            data: res.data.msg,
            success: function (res) {
              // self.setData({copyTip:true}),
              wx.showModal({
                title: '复制成功',
                content: '是否跳转去播放',
                success: function (res) {
                  if (res.confirm) {
                    // 跳转直播页面
                    wx.redirectTo({
                      url: '../push/index',
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          });
        } else {
          utils.showError('待审核');
        }
      },
      fail: function (res) {
        utils.showError(res.message)
      },
      complete: function () {
       
      }
    })
    
  },
  // 删除播放
  liveDel: function() {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定要删除吗',
      success: function(res) {
        if (res.confirm) {
          qcloud.request({
            login: false,
            url: config.service.liveDelUrl,
            data: {
              id: that.data.listData[that.data.shiPid].id
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              utils.showError('删除成功');
              that.setData({
                showTanBox: false
              })
              setTimeout(function () {
                that.setData({
                  page: 1,
                  listData: []
                })
                that.indexListData();
              },500)
            },
            fail: function (res) {
              utils.showError(res.message)
            },
            complete: function () {
              setTimeout(function () {
                wx.hideLoading();
                wx.stopPullDownRefresh();
              }, 500)
            }
          });
        } else if (res.cancel) {

        }
      }
    })
  },
  //结束直播
  delZhiBo: function(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定要关闭播放吗',
      success: function(res) {
        if (res.confirm) {
          qcloud.request({
            login: false,
            url: config.service.finishUrl,
            data: {
              id: that.data.listData[that.data.shiPid].id
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function(res) {
              utils.showError('结束成功');
              that.setData({
                showTanBox: false
              })
            },
            fail: function(res) {
              utils.showError(res.message)
            },
            complete: function() {
              setTimeout(function() {
                wx.hideLoading();
                wx.stopPullDownRefresh();
              }, 500)
            }
          });
        } else if (res.cancel) {

        }
      }
    })
  },
  clickHideTanBox: function() {
    this.setData({
      showTanBox: false
    })
  },
  // 首页列表数据请求
  indexListData: function() {
    var that = this;
    qcloud.request({
      login: false,
      url: config.service.liveArrayUrl,
      data: {
        page: that.data.page,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        var arr = that.data.listData;
        arr = arr.concat(that._formatData(res.data.data.data));
        if (that.data.listData.length !== 0) {
          if (res.data.data.data.length == 0) {
            // 分页没有数据了，显示提示框
            that.setData({
              dataOver: true
            })
          } else {
            that.setData({
              page: that.data.page + 1,
              listData: arr,
            });
          }
        } else {
          that.setData({
            dataOver: true,
            listData: arr,
            page: that.data.page + 1,
          })
        }

      },
      fail: function(res) {
        utils.showError('请求出错')
      },
      complete: function() {
        setTimeout(function() {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    });

  },
  //时间转换
  _formatData: function(dataArray) {
    var that = this; 
    for (var i in dataArray) {
      dataArray[i].stime = commons.dateTools.timestampToDataDayTwo(dataArray[i].stime);
    }
    return dataArray;
  },
  //弹出操作
  clickTanShowBox: function(e) {
    var shiPid =  e.currentTarget.id;
    var stauts = this.data.listData[shiPid].stauts;
    // stauts 0未审核 1是已审核 2直播 3是回播 4是转码中  
    this.setData({
      shiPid: shiPid,
      stautsPanDuan: stauts,
      showTanBox: true,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      listData: []
    })
    this.indexListData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.indexListData();
  },
};
var data = Object.assign(index.data, register.data);
var page = Object.assign(index, register);
page.data = data;
Page(page);